set -e 

newperms() { # Set special sudoers settings for install (or after).
	sed -i "/#WILLOWS/d" /etc/sudoers
	echo "$* #WILLOWS" >> /etc/sudoers
}

error() { 
    printf "%s\n" "$1" >&2; exit 1 
}

get_repos() {
    if [ $(yq -r .repos.nonfree "${PROFILE}") = "true" ]; then
        echo "void-repo-nonfree "
    fi
    if  [ $(yq -r .repos.multilib "${PROFILE}") = "true" ]; then
        echo "void-repo-multilib void-repo-multilib-nonfree "
    fi
    if [ $(yq -r .repos.debug "${PROFILE}") = "true" ]; then
        echo "void-repo-debug "
    fi
}

get_packages() {
    yq -r .packages.base.boot."${BOOT_TYPE}" "${PROFILE}"
    yq -r .packages.base.system "${PROFILE}"
    yq -r .packages.base.hardware.cpu.$(yq -r .config.cpu "${PROFILE}") "${PROFILE}"
    yq -r .packages.base.hardware.gpu.$(yq -r .config.gpu "${PROFILE}") "${PROFILE}"
    yq -r .packages.base.hardware.misc "${PROFILE}"
    
    yq -r .packages.desktop.$(yq -r .config.desktop "${PROFILE}").minimal "${PROFILE}"
    yq -r .packages.desktop.$(yq -r .config.desktop "${PROFILE}").full "${PROFILE}"
    yq -r .packages.desktop.$(yq -r .config.desktop "${PROFILE}").extra "${PROFILE}"
   
    yq -r .packages.shell.zsh.minimal "${PROFILE}"
    yq -r .packages.shell.zsh.full "${PROFILE}"
    yq -r .packages.shell.zsh.extra "${PROFILE}"
    
    yq -r .packages.development.minimal "${PROFILE}"
    yq -r .packages.development.full "${PROFILE}"
    yq -r .packages.development.extra "${PROFILE}"
    
    yq -r .packages.audio.minimal "${PROFILE}"
   
    yq -r .packages.applications.minimal "${PROFILE}"
    yq -r .packages.applications.full "${PROFILE}"
    yq -r .packages.applications.extra "${PROFILE}"
    
    yq -r .packages.fonts.minimal "${PROFILE}"
    yq -r .packages.fonts.full "${PROFILE}"
    
    yq -r .packages.misc.full "${PROFILE}"
}

get_fsopts() {
    opts="compress=$(yq -r .config.advanced.filesystem.compress "${PROFILE}"),commit=$(yq -r .config.advanced.filesystem.commit "${PROFILE}"),$(yq -r .config.advanced.filesystem.extra_opts "${PROFILE}")"
    if [ "$(yq -r .config.advanced.filesystem.ssd "${PROFILE}")" = "true" ]; then
        opts="${opts},ssd"
    fi
    echo "${opts}"
}

xbps-install -Sy yq python3 python3-pip

if [ "${1}" = "" ]; then
    echo "Using default profile at /willows/config.yaml"
    PROFILE="/willows/config.yaml"
elif [ -f "${1}" ]; then
    echo "Using profile ${1}"
    PROFILE="${1}"
else
    error "Unable to find profile ${1}"
fi

AUTO_PARTITION=yes
DEVICE=$(yq -r .config.advanced.partitions.device "${PROFILE}")
BOOT="${DEVICE}$(yq -r .config.advanced.partitions.boot ${PROFILE})"
ROOT="${DEVICE}$(yq -r .config.advanced.partitions.root ${PROFILE})"
#HOME=$(yq -r .config.advanced.disks.home "${PROFILE}")

ARCH=$(yq -r .config.arch "${PROFILE}")
KEYMAP=$(yq -r .config.keymap "${PROFILE}")
LOCALE=$(yq -r .config.locale "${PROFILE}")
TIMEZONE=$(yq -r .config.timezone "${PROFILE}")
HWCLOCK=$(yq -r .config.advanced.hwclock "${PROFILE}")

FS_OPTS=$(get_fsopts)
BOOT_TYPE=$(yq -r .config.advanced.boot_type "${PROFILE}")

MIRROR=$(yq -r .config.advanced.mirror "${PROFILE}")
PACKAGES=$(get_packages)
REPOS=$(get_repos)

USER_GROUPS=$(yq -r .config.advanced.user_groups "${PROFILE}")
USERNAME=$(yq -r .config.username "${PROFILE}")
HOSTNAME=$(yq -r .config.hostname "${PROFILE}")

DESKTOP=$(yq -r .config.desktop "${PROFILE}")
DISPLAY_MANAGER=$(yq -r .config.display_manager "${PROFILE}")

printf "\n** System **\n"
echo "Arch: ${ARCH}"
echo "Keys: ${KEYMAP}"
echo "Locale: ${LOCALE}"
echo "Timezone: ${TIMEZONE}"
echo "Hostname: ${HOSTNAME}"
echo "Boot Type: ${BOOT_TYPE}"

printf "\n** Files **\n"
echo "Filesystem Options: ${FS_OPTS}"
echo "Device: ${DEVICE}"
echo "Boot: ${BOOT}"
echo "Root: ${ROOT}"

printf "\n** Packages **\n"
echo "Package Mirror: ${MIRROR}"
echo "Repositories: ${REPOS}"
echo "Packages: ${PACKAGES}"

printf "\n** User **\n"
echo "Username: ${USERNAME}"
echo "Groups: ${USER_GROUPS}"

printf "\n** GUI Enviroment **\n"
echo "Desktop Enviroment: ${DESKTOP}"
echo "Login Manager: ${DISPLAY_MANAGER}"
